﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.QPZS.EPC.API.Domain.Entities;
using Wicture.QPZS.EPC.API.Repository;

namespace Wicture.QPZS.EPC.API.Throttlings
{
    public class AccountBalanceThrotting : IThrottling
    {
        private readonly IServiceRepository _serviceRepository;

        public AccountBalanceThrotting(IServiceRepository serviceRepository)
        {
            _serviceRepository = serviceRepository;
        }

        public decimal Order => 3;

        public List<ServicePackageType> ServicePackageTypes => new List<ServicePackageType> {
            ServicePackageType.计价
        };

        public void Invoke(ThrottlingContext context)
        {
            var account = _serviceRepository.GetAccount(context.AccountId);
            if (account.Balance <= 0)
            {
                throw new LogicalException($"账户余额不足。", 602);
            }
        }
    }
}
